package com.sinoglobal.ningxia.activity;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.adapter.MyCollectionFragmentAdapter;
import com.sinoglobal.ningxia.beans.MyCollectionDataVo;
import com.sinoglobal.ningxia.beans.MyCollectionNumVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.fragment.MyCollectionColumnFragment;
import com.sinoglobal.ningxia.fragment.MyCollectionNewsFragment;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.ToastBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wxy
 * @createdate 2014-6-16 上午9:50:31
 * @Description: 左侧菜单栏点击“我的收藏”进入此页面
 */
public class MyCollectionActivity extends FragmentActivity implements OnClickListener {
    public static final int TAB_NEWS = 0;// 新闻
    public static final int TAB_COLUMN = 1;// 栏目

    private ImageView my_collection_main_title_back_img;// 我的收藏界面的返回按钮
    private List<Fragment> lists;// 用于存放5中类型的界面
    private MyCollectionNewsFragment myCollectionNewsFragment;// 新闻界面
    private MyCollectionColumnFragment myCollectionColumnFragment;// 栏目界面

    private ViewPager viewPager;// 5个界面所展示的viewpager
    private RadioButton my_collection_news; // 新闻
    private RadioButton my_collection_column;// 栏目
    private final String CATE_TYPE = "2";
    private ArrayList<MyCollectionNumVo> collectionNumVos;// 返回所有type的数量值的type_nums

    public static Handler myCollectionTabTextNumHandler;// 用于5中界面获取到收藏数之后，显示在Tab上
    private MyCollectionFragmentAdapter myCollectionFragmentAdapter;// 将5个不同的fragment通过Adapter适配上去

    private ProgressDialog pd;
    private String zero = "0";//0条记录
    private final String START_PAGE = "0";
    private final String PER_PAGE_NUM = "10";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_collection);
        initView();// 初始化控件
        loadData(CATE_TYPE);// 初次进来加载页面数据,默认加载type为2即美食的数据,主要是用来显示tab栏上面的所有数字。
        addListener();// 添加点击事件
        initState();
    }

    /**
     * 沉浸式状态栏
     */
    public void initState() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.parseColor("#dd191d"));
        }

    }

    private void loadData(final String type) {
        new AsyncTask<Void, Void, MyCollectionDataVo>() {

            protected void onPreExecute() {
                showWaitingDialog(getString(R.string.get_list_collect), true, true, true);
                super.onPreExecute();
            }

            ;

            @Override
            protected MyCollectionDataVo doInBackground(Void... params) {
                try {
                    return RemoteImpl.getInstance().getMyCollectionDataVo(FlyApplication.userId, type, START_PAGE, PER_PAGE_NUM);
                } catch (Exception e) {
                    //Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(MyCollectionDataVo result) {
                //Auto-generated method stub
                pd.dismiss();
                if (result != null) {
                    if (Code.CODE_SUCCESS != result.getCode()) {
                        ToastBuilder.getIntance(FlyApplication.context).showToast(result.getMessage());
                    } else {
                        collectionNumVos = result.getType_nums();
                        ItktLog.i("collectionNumVos的长度" + collectionNumVos.size());
                        // 将2种类型的数量显示在tab上

                        if (!zero.equals(collectionNumVos.get(2).getNums())) {
                            my_collection_news.setText(getString(R.string.my_collection_news) + "(" + collectionNumVos.get(2).getNums()
                                    + ")");
                        } else {
                            my_collection_news.setText(getString(R.string.my_collection_news));
                        }
                        if (!zero.equals(collectionNumVos.get(3).getNums())) {
                            my_collection_column.setText(getString(R.string.my_collection_column) + "(" + collectionNumVos.get(3).getNums()
                                    + ")");
                        } else {
                            my_collection_column.setText(getString(R.string.my_collection_column));
                        }


                    }
                }
                super.onPostExecute(result);

            }
        }.execute();
    }

    @SuppressLint("HandlerLeak")
    private void initView() {
        // 将3个fragment初始化，并且放在ArrayList中
        lists = new ArrayList<Fragment>();
        myCollectionNewsFragment = new MyCollectionNewsFragment();
        myCollectionColumnFragment = new MyCollectionColumnFragment();
        lists.add(myCollectionNewsFragment);
        lists.add(myCollectionColumnFragment);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        my_collection_main_title_back_img = (ImageView) findViewById(R.id.my_collection_main_title_back_img);
        my_collection_news = (RadioButton) findViewById(R.id.my_collection_news);
        my_collection_column = (RadioButton) findViewById(R.id.my_collection_column);
        my_collection_news.setOnClickListener(this);
        my_collection_column.setOnClickListener(this);

        // 给viewPager设置Adapter
        myCollectionFragmentAdapter = new MyCollectionFragmentAdapter(getSupportFragmentManager());
        myCollectionFragmentAdapter.setLists(lists);
        viewPager.setAdapter(myCollectionFragmentAdapter);

        // 默认娱乐被选中,设置颜色值
        my_collection_news.setTextColor(getResources().getColor(R.color.red));

        myCollectionTabTextNumHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    // 由其他界面初始化时候传过来的数量值
                    case TAB_NEWS:
                        if (!zero.equals(FlyApplication.myCollectionTabNewsNum)) {

                            my_collection_news.setText(getString(R.string.my_collection_news) + "(" + FlyApplication.myCollectionTabNewsNum
                                    + ")");
                        } else {
                            my_collection_news.setText(getString(R.string.my_collection_news));
                        }
                        break;
                    case TAB_COLUMN:

                        if (!zero.equals(FlyApplication.myCollectionTabColumnNum)) {

                            my_collection_column.setText(getString(R.string.my_collection_column) + "("
                                    + FlyApplication.myCollectionTabColumnNum + ")");
                        } else {
                            my_collection_column.setText(getString(R.string.my_collection_column));
                        }
                        break;

                    default:
                        break;
                }
            }
        };
    }

    private void addListener() {
        // 返回按钮
        my_collection_main_title_back_img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyCollectionActivity.this.finish();

            }
        });

        // 滑动切换不同的标签，由adaper决定id
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int id) {
                switch (id) {
                    case TAB_NEWS:
                        // 新闻被选中,以及颜色的变化
                        my_collection_news.setChecked(true);
                        my_collection_news.setTextColor(getResources().getColor(R.color.red));
                        my_collection_column.setTextColor(getResources().getColor(R.color.myCollectionTabTextColor));
                        break;
                    case TAB_COLUMN:
                        // 栏目被选中,以及颜色的变化
                        my_collection_column.setChecked(true);
                        my_collection_news.setTextColor(getResources().getColor(R.color.myCollectionTabTextColor));
                        my_collection_column.setTextColor(getResources().getColor(R.color.red));
                        break;

                    default:
                        break;
                }

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                //Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                //Auto-generated method stub

            }
        });

    }

    @Override
    public void onClick(View v) {
        // 点击不同的标签设置不同的id
        switch (v.getId()) {

            case R.id.my_collection_news:// 新闻
                viewPager.setCurrentItem(TAB_NEWS);
                break;
            case R.id.my_collection_column:// 栏目
                viewPager.setCurrentItem(TAB_COLUMN);
                break;


            default:
                break;
        }

    }

    public void showWaitingDialog(String message, boolean cancelable, boolean flag, boolean isLoadingPrompt) {

        pd = ProgressDialog.show(this, null, message, true, cancelable);
    }

}
